{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Numpy核心语法和代码整理汇总\n",
    "***\n",
    "***\n",
    "Time: 2020-09-08 <br>\n",
    "Author: dsy <br>\n",
    "Notes: [Numpy核心语法和代码整理汇总](https://mp.weixin.qq.com/s?__biz=MzU1MzA4MzA4NQ==&mid=2247486753&idx=2&sn=027ede14eeabdf01df3c27099475a77c&chksm=fbf90483cc8e8d958b9f1f83c1c4d35af576247d5b5feb105488cbf29986bf24bdab184ed35f&mpshare=1&scene=23&srcid=09082CRXm9w1MEQiGZPwowQ5&sharer_sharetime=1599537116981&sharer_shareid=7ea5c15a13f4f0977d4dd7d336f6780f#rd)\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 安装`Numpy`\n",
    "\n",
    "`pip install numpy `或者 `conda install numpy`\n",
    "    ```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NumPy最常用的功能之一就是NumPy数组：列表和NumPy数组的最主要区别在于功能性和速度。\n",
    "\n",
    "列表提供基本操作，但NumPy添加了FTTs、卷积、快速搜索、基本统计、线性代数、直方图等。\n",
    "\n",
    "两者数据科学最重要的区别是能够用NumPy数组进行元素级计算。\n",
    "\n",
    "* axis 0：通常指行\n",
    "\n",
    "* axis 1：通常指列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![array](./imgs/array.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.1 占位符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![占位符](./imgs/占位符.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2. 数组属性"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![数组属性](./imgs/数组属性.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.3 拷贝 /排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![拷贝_排序](./imgs/拷贝_排序.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.4 数组操作例程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "增加或减少元素\n",
    "\n",
    "![数组操作例程](./imgs/增加或减少元素.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "组合数组\n",
    "\n",
    "![组合数组](./imgs/组合数组.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分割数组\n",
    "\n",
    "![分割数组](./imgs/分割数组.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数组形状变化\n",
    "\n",
    "![数组形状变化](./imgs/数组形状变化.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其他\n",
    "\n",
    "![其他](./imgs/其他.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.5 数学计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "操作\n",
    "\n",
    "![操作](./imgs/操作.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2.,  2.])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.roots([1,0,-4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "1* x^ 2 + 0 * x ^ 1 -4 * x ^ 0 \\\\ \n",
    "当上面的为零时候，x_1 = 2,x_2 = -2\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "比较\n",
    "\n",
    "![比较](./imgs/比较.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基本的统计\n",
    "\n",
    "![基本的统计](./imgs/基本的统计.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.median(np.array([1,2,3])) # np.median()计算中位数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "更多\n",
    "\n",
    "![更多](./imgs/更多.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2],\n",
       "       [ 3,  4],\n",
       "       [ 5,  6],\n",
       "       [ 7,  8],\n",
       "       [ 9, 10]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "x = np.arange(1,11,1).reshape((5,2))\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2],\n",
       "       [ 4,  6],\n",
       "       [ 9, 12],\n",
       "       [16, 20],\n",
       "       [25, 30]], dtype=int32)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.cumsum(axis=0) # 以行进行累计和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.6 切片和子集"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![切片和子集](./imgs/切片和子集.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.7 小技巧"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Index trick when working with two np-arrays\n",
    "a = np.array([1,2,3,6,1,4,1])\n",
    "b = np.array([5,6,7,8,3,1,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False,  True, False])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[b==1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 6, 1, 1])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[b !=1 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 6, 8, 1, 2, 6, 9])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "x = np.array([4,6,8,1,2,6,9])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False,  True,  True, False, False,  True,  True])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x > 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 8, 6, 9])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[x>5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 4])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1, 2, 3, 4, 4, 35, 212, 5, 5, 6])\n",
    "x[x < 5]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
